package sbpme.designer.command;

import java.util.List;

import org.eclipse.gef.commands.Command;

import sbpme.designer.model.Activity;
import sbpme.designer.model.Transition;

public class TransitionCreateCommand extends Command {

	protected Transition transition;
	protected Activity source;
	protected Activity target;

	public boolean canExecute() {
		if (source.equals(target))
			return false;

		// Check for existence of connection already
		List transistions = source.getTargetTransitions();
		for (int i = 0; i < transistions.size(); i++) {
			if (((Transition) transistions.get(i)).getTarget().equals(target))
				return false;
		}
		return true;
	}
	
	public void execute() {
		transition = new Transition(source, target);
		transition.notifyObservers();
	}

	public void undo() {
		source.removeTargetTransition(transition);
		target.removeSourceTransition(transition);
	}
	
	public void redo() {
		source.addTargetTransition(transition);
		target.addSourceTransition(transition);
	}

	public Activity getSource() {
		return source;
	}

	public Activity getTarget() {
		return target;
	}

	public Transition getTransition() {
		return transition;
	}

	public void setSource(Activity activity) {
		source = activity;
	}

	public void setTransition(Transition transition) {
		this.transition = transition;
	}

	public void setTarget(Activity activity) {
		target = activity;
	}

}
